package com.global.college.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.global.college.R;

/**
 * 自定义dialog，含一行文本及两个按钮
 */
public class OneLineDialog {

    private Dialog dialog;
    private TextView message;
    private Button firstBtn;
    private Button secondBtn;
    private onButtnClickListener buttnClickListener;

    public OneLineDialog(Context context) {
        initDialog(context);
    }

    public void setMessage(String message) {
        if (null != message && !message.equals("")) {
            this.message.setText(message);
        }

    }

    public void setTextDrawable(Drawable drawable){
        message.setCompoundDrawables(drawable,null,null,null);
        message.setCompoundDrawablePadding(20);
    }

    public void setLeftButtonText(String text){
        firstBtn.setText(text);
    }

    public void setRightButtonText(String text){
        secondBtn.setText(text);
    }

    private void initDialog(Context context) {
        dialog = new Dialog(context);

        dialog.setContentView(R.layout.dialog_layout);
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        dialog.setCanceledOnTouchOutside(true);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = ScreenUtils.getScreenWidth(context)
                - ScreenUtils.getScreenWidth(context) / 4;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
        window.setWindowAnimations(R.style.dialog);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        message = (TextView) dialog.findViewById(R.id.txt_dialog_message);
        firstBtn = (Button) dialog.findViewById(R.id.btn_dialog_go);
        secondBtn = (Button) dialog.findViewById(R.id.btn_dialog_add);
        firstBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (null != buttnClickListener){
                    buttnClickListener.onFirstBtnClick(v);
                }

            }
        });
        secondBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (null != buttnClickListener){
                    buttnClickListener.onSecondBtnClick(v);
                }

            }
        });
    }

    public void showDialog() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void dissmissDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();

        }
    }

    public void setOnButtnClickListener(onButtnClickListener buttnClickListener) {
        this.buttnClickListener = buttnClickListener;
    }

    public interface onButtnClickListener {
        public void onFirstBtnClick(View v);

        public void onSecondBtnClick(View v);
    }

}